
' This file implements the TableControl, TableControlRow, and RecordControl classes for the 
' EditEmployeesPage.aspx page.  The Row or RecordControl classes are the 
' ideal place to add code customizations. For example, you can override the LoadData, 
' CreateWhereClause, DataBind, SaveData, GetUIData, and Validate methods.

#Region "Imports statements"

Option Strict On
Imports Microsoft.VisualBasic
Imports BaseClasses.Web.UI.WebControls
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports BaseClasses
Imports BaseClasses.Data
Imports BaseClasses.Utils
Imports ReportTools.ReportCreator
Imports ReportTools.Shared

  
        
Imports Training1.Business
Imports Training1.Data
        

#End Region

  
Namespace Training1.UI.Controls.EditEmployeesPage

#Region "Section 1: Place your customizations here."

    
Public Class EmployeesRecordControl
        Inherits BaseEmployeesRecordControl
        ' The BaseEmployeesRecordControl implements the LoadData, DataBind and other
        ' methods to load and display the data in a table control.

        ' This is the ideal place to add your code customizations. For example, you can override the LoadData, 
        ' CreateWhereClause, DataBind, SaveData, GetUIData, and Validate methods.
        

End Class

  

#End Region

  

#Region "Section 2: Do not modify this section."
    
    
' Base class for the EmployeesRecordControl control on the EditEmployeesPage page.
' Do not modify this class. Instead override any method in EmployeesRecordControl.
Public Class BaseEmployeesRecordControl
        Inherits Training1.UI.BaseApplicationRecordControl

        '  To customize, override this method in EmployeesRecordControl.
      Protected Overridable Sub Control_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init

      
      ' Setup the pagination events.
      
          
        ' Setup the filter and search events.
        
      
            ' Register the event handlers.
        
        End Sub

        '  To customize, override this method in EmployeesRecordControl.
        Protected Overridable Sub Control_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        End Sub

        ' Read data from database.  To customize, override this method in EmployeesRecordControl.
        Public Overridable Sub LoadData()        
        
            If Not Me.RecordUniqueId Is Nothing AndAlso Me.RecordUniqueId.Trim <> "" Then
                Me.DataSource = EmployeesTable.GetRecord(Me.RecordUniqueId, True)
                Return
            End If
        
            Dim wc As WhereClause = Me.CreateWhereClause()
            
            If wc Is Nothing OrElse Not wc.RunQuery Then
                Me.DataSource = New EmployeesRecord()
                
                Return
            End If
            

          
              
            ' Retrieve the record from the database.
            
            Dim recList() As EmployeesRecord = EmployeesTable.GetRecords(wc, Nothing, 0, 2)
            If recList.Length = 0 Then
                Throw New Exception(Page.GetResourceValue("Err:NoRecRetrieved", "Training1"))
            End If
              
            Me.DataSource = EmployeesTable.GetRecord(recList(0).GetID.ToXmlString(), True)
                  
        End Sub

        ' Populate the UI controls using the DataSource.  To customize, override this method in EmployeesRecordControl.
        Public Overrides Sub DataBind()

            MyBase.DataBind()

            ' Make sure that the DataSource is initialized.
            If Me.DataSource Is Nothing Then
                Return
            End If
        
      
            ' For each field, check to see if a value is specified.  If a value is specified,
            ' then format the value for display.  If no value is specified, use the default value (formatted).

        
            If Me.DataSource.LastNameSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.LastName)
                Me.LastName.Text = formattedValue
              
            Else      
                Me.LastName.Text = EmployeesTable.LastName.Format(EmployeesTable.LastName.DefaultValue)
              
            End If
                    
            If Me.DataSource.FirstNameSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.FirstName)
                Me.FirstName.Text = formattedValue
              
            Else      
                Me.FirstName.Text = EmployeesTable.FirstName.Format(EmployeesTable.FirstName.DefaultValue)
              
            End If
                    
            If Me.DataSource.TitleSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.Title)
                Me.Title.Text = formattedValue
              
            Else      
                Me.Title.Text = EmployeesTable.Title.Format(EmployeesTable.Title.DefaultValue)
              
            End If
                    
            If Me.DataSource.TitleOfCourtesySpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.TitleOfCourtesy)
                Me.TitleOfCourtesy.Text = formattedValue
              
            Else      
                Me.TitleOfCourtesy.Text = EmployeesTable.TitleOfCourtesy.Format(EmployeesTable.TitleOfCourtesy.DefaultValue)
              
            End If
                    
            Me.BirthDate.Attributes.Add("onkeyup", "DateFormat(this, '', event, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "')")
            Me.BirthDate.Attributes.Add("onfocus", "toggleEnableDisableDateFormatter(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
            Me.BirthDate.Attributes.Add("onblur", "presubmitDateValidation(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
                    
            If Me.DataSource.BirthDateSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.BirthDate)
                Me.BirthDate.Text = formattedValue
              
            Else      
                Me.BirthDate.Text = EmployeesTable.BirthDate.Format(EmployeesTable.BirthDate.DefaultValue)
              
            End If
                    
            Me.HireDate.Attributes.Add("onkeyup", "DateFormat(this, '', event, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "')")
            Me.HireDate.Attributes.Add("onfocus", "toggleEnableDisableDateFormatter(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
            Me.HireDate.Attributes.Add("onblur", "presubmitDateValidation(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
                    
            If Me.DataSource.HireDateSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.HireDate)
                Me.HireDate.Text = formattedValue
              
            Else      
                Me.HireDate.Text = EmployeesTable.HireDate.Format(EmployeesTable.HireDate.DefaultValue)
              
            End If
                    
            If Me.DataSource.AddressSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.Address)
                Me.Address.Text = formattedValue
              
            Else      
                Me.Address.Text = EmployeesTable.Address.Format(EmployeesTable.Address.DefaultValue)
              
            End If
                    
            If Me.DataSource.CitySpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.City)
                Me.City.Text = formattedValue
              
            Else      
                Me.City.Text = EmployeesTable.City.Format(EmployeesTable.City.DefaultValue)
              
            End If
                    
            If Me.DataSource.RegionSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.Region)
                Me.Region.Text = formattedValue
              
            Else      
                Me.Region.Text = EmployeesTable.Region.Format(EmployeesTable.Region.DefaultValue)
              
            End If
                    
            If Me.DataSource.PostalCodeSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.PostalCode)
                Me.PostalCode.Text = formattedValue
              
            Else      
                Me.PostalCode.Text = EmployeesTable.PostalCode.Format(EmployeesTable.PostalCode.DefaultValue)
              
            End If
                    
            If Me.DataSource.CountrySpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.Country)
                Me.Country.Text = formattedValue
              
            Else      
                Me.Country.Text = EmployeesTable.Country.Format(EmployeesTable.Country.DefaultValue)
              
            End If
                    
            If Me.DataSource.HomePhoneSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.HomePhone)
                Me.HomePhone.Text = formattedValue
              
            Else      
                Me.HomePhone.Text = EmployeesTable.HomePhone.Format(EmployeesTable.HomePhone.DefaultValue)
              
            End If
                    
            If Me.DataSource.ExtensionSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.Extension)
                Me.Extension.Text = formattedValue
              
            Else      
                Me.Extension.Text = EmployeesTable.Extension.Format(EmployeesTable.Extension.DefaultValue)
              
            End If
                    
            If Me.DataSource.NotesSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.Notes)
                Me.Notes.Value = formattedValue
                Me.Notes.DefaultLanguage = System.Threading.Thread.CurrentThread.CurrentUICulture.Name
              
            Else      
                Me.Notes.DefaultLanguage = System.Threading.Thread.CurrentThread.CurrentUICulture.Name
                Me.Notes.Value = EmployeesTable.Notes.Format(EmployeesTable.Notes.DefaultValue)
              
            End If
                    
            If Me.DataSource.ReportsToSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.ReportsTo)
                Me.ReportsTo.Text = formattedValue
              
            Else      
                Me.ReportsTo.Text = EmployeesTable.ReportsTo.Format(EmployeesTable.ReportsTo.DefaultValue)
              
            End If
                    
            Me.CreatedOn.Attributes.Add("onkeyup", "DateFormat(this, '', event, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "')")
            Me.CreatedOn.Attributes.Add("onfocus", "toggleEnableDisableDateFormatter(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
            Me.CreatedOn.Attributes.Add("onblur", "presubmitDateValidation(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
                    
            If Me.DataSource.CreatedOnSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.CreatedOn)
                Me.CreatedOn.Text = formattedValue
              
            Else      
                Me.CreatedOn.Text = EmployeesTable.CreatedOn.Format(EmployeesTable.CreatedOn.DefaultValue)
              
            End If
                    
            If Me.DataSource.CreatedBySpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.CreatedBy)
                Me.CreatedBy.Text = formattedValue
              
            Else      
                Me.CreatedBy.Text = EmployeesTable.CreatedBy.Format(EmployeesTable.CreatedBy.DefaultValue)
              
            End If
                    
            Me.LastUpdatedOn.Attributes.Add("onkeyup", "DateFormat(this, '', event, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "')")
            Me.LastUpdatedOn.Attributes.Add("onfocus", "toggleEnableDisableDateFormatter(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
            Me.LastUpdatedOn.Attributes.Add("onblur", "presubmitDateValidation(this, '" & System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("'", "").ToLower() & "');")
                    
            If Me.DataSource.LastUpdatedOnSpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.LastUpdatedOn)
                Me.LastUpdatedOn.Text = formattedValue
              
            Else      
                Me.LastUpdatedOn.Text = EmployeesTable.LastUpdatedOn.Format(EmployeesTable.LastUpdatedOn.DefaultValue)
              
            End If
                    
            If Me.DataSource.LastUpdatedBySpecified Then
                Dim formattedValue As String = Me.DataSource.Format(EmployeesTable.LastUpdatedBy)
                Me.LastUpdatedBy.Text = formattedValue
              
            Else      
                Me.LastUpdatedBy.Text = EmployeesTable.LastUpdatedBy.Format(EmployeesTable.LastUpdatedBy.DefaultValue)
              
            End If
                    
            Me.IsNewRecord = True
            If Me.DataSource.IsCreated Then
                Me.IsNewRecord = False
        
                Me.RecordUniqueId = Me.DataSource.GetID.ToXmlString()
            End If

            

            ' Load data for each record and table UI control.
            ' Ordering is important because child controls get 
            ' their parent ids from their parent UI controls.
            
      End Sub


      
        
        ' To customize, override this method in EmployeesRecordControl.
        Public Overridable Sub SaveData()

            ' 1. Load the existing record from the database. Since we save the entire record, this ensures 
            ' that fields that are not displayed also properly initialized.
            Me.LoadData()
        
            ' 2. Validate the data.  Override in EmployeesRecordControl to add custom validation.
            Me.Validate()

            ' 3. Set the values in the record with data from UI controls.  Override in EmployeesRecordControl to set additional fields.
            Me.GetUIData()

            ' 4. Save in the database.
            ' We should not save the record if the data did not change. This
            ' will save a database hit and avoid triggering any database triggers.
            If Me.DataSource.IsAnyValueChanged Then
                ' Save record to database but do not commit.
                ' Auto generated ids are available after saving for use by child (dependent) records.
                Me.DataSource.Save()
              
            End If
            Me.DataChanged = True
            Me.ResetData = True
            ' Reseting of Me.IsNewRecord is moved to Save button's click even handler.
            ' Me.IsNewRecord() = False
            
        End Sub

        ' To customize, override this method in EmployeesRecordControl.
        Public Overridable Sub GetUIData()
            
            Me.DataSource.Parse(Me.LastName.Text, EmployeesTable.LastName)
                          
            Me.DataSource.Parse(Me.FirstName.Text, EmployeesTable.FirstName)
                          
            Me.DataSource.Parse(Me.Title.Text, EmployeesTable.Title)
                          
            Me.DataSource.Parse(Me.TitleOfCourtesy.Text, EmployeesTable.TitleOfCourtesy)
                          
            Me.DataSource.Parse(Me.BirthDate.Text, EmployeesTable.BirthDate)
                          
            Me.DataSource.Parse(Me.HireDate.Text, EmployeesTable.HireDate)
                          
            Me.DataSource.Parse(Me.Address.Text, EmployeesTable.Address)
                          
            Me.DataSource.Parse(Me.City.Text, EmployeesTable.City)
                          
            Me.DataSource.Parse(Me.Region.Text, EmployeesTable.Region)
                          
            Me.DataSource.Parse(Me.PostalCode.Text, EmployeesTable.PostalCode)
                          
            Me.DataSource.Parse(Me.Country.Text, EmployeesTable.Country)
                          
            Me.DataSource.Parse(Me.HomePhone.Text, EmployeesTable.HomePhone)
                          
            Me.DataSource.Parse(Me.Extension.Text, EmployeesTable.Extension)
                          
                            Me.DataSource.Parse(Me.Notes.Value, EmployeesTable.Notes)
                          
            Me.DataSource.Parse(Me.ReportsTo.Text, EmployeesTable.ReportsTo)
                          
            Me.DataSource.Parse(Me.CreatedOn.Text, EmployeesTable.CreatedOn)
                          
            Me.DataSource.Parse(Me.CreatedBy.Text, EmployeesTable.CreatedBy)
                          
            Me.DataSource.Parse(Me.LastUpdatedOn.Text, EmployeesTable.LastUpdatedOn)
                          
            Me.DataSource.Parse(Me.LastUpdatedBy.Text, EmployeesTable.LastUpdatedBy)
                          
        End Sub

        
      
        ' To customize, override this method in EmployeesRecordControl.
        Public Overridable Function CreateWhereClause() As WhereClause
        
            Dim wc As WhereClause
            EmployeesTable.Instance.InnerFilter = Nothing
            wc = New WhereClause()
            ' Compose the WHERE clause consiting of:
            ' 1. Static clause defined at design time.
            ' 2. User selected filter criteria.
            ' 3. User selected search criteria.
            
            
            ' Retrieve the record id from the URL parameter.
              
            Dim recId As String = Me.Page.Request.QueryString.Item("Employees")
                
            If recId Is Nothing OrElse recId.Trim = "" Then
                ' Get the error message from the application resource file.
                Throw New Exception(Page.GetResourceValue("Err:UrlParamMissing", "Training1").Replace("{URL}", "Employees"))
            End If
            HttpContext.Current.Session("SelectedID") = recId
              
            If KeyValue.IsXmlKey(recId) Then
                Dim pkValue As KeyValue = KeyValue.XmlToKey(recId)
                
                wc.iAND(EmployeesTable.EmployeeID, BaseFilter.ComparisonOperator.EqualsTo, pkValue.GetColumnValue(EmployeesTable.EmployeeID).ToString())
            Else
                
                wc.iAND(EmployeesTable.EmployeeID, BaseFilter.ComparisonOperator.EqualsTo, recId)
            End If
              

            
            Return wc
          
        End Function
        
        ' This CreateWhereClause is used for loading list of suggestions for Auto Type-Ahead feature.
        Public Overridable Function CreateWhereClause(ByVal searchText as String, ByVal fromSearchControl as String, ByVal AutoTypeAheadSearch as String, ByVal AutoTypeAheadWordSeparators as String) As WhereClause
            EmployeesTable.Instance.InnerFilter = Nothing
            Dim wc As WhereClause = New WhereClause()
            ' Compose the WHERE clause consiting of:
            ' 1. Static clause defined at design time.
            ' 2. User selected filter criteria.
            ' 3. User selected search criteria.
            Dim appRelativeVirtualPath As String = CType(HttpContext.Current.Session("AppRelatvieVirtualPath"), String)
          
            ' Adds clauses if values are selected in Filter controls which are configured in the page.
          
            Return wc
        End Function
          
          
        'Formats the resultItem and adds it to the list of suggestions.
        Public Overridable Function FormatSuggestions(ByVal prefixText As String, ByVal resultItem as String, _
                                                 ByVal columnLength As Integer, ByVal AutoTypeAheadDisplayFoundText as String, _
                                                 ByVal autoTypeAheadSearch as String, ByVal AutoTypeAheadWordSeparators as String, _
                                                 ByVal resultList as ArrayList) as Boolean
            Dim index As Integer = InvariantLCase(resultItem).IndexOf(InvariantLCase(prefixText))
            Dim itemToAdd As String = ""
            Dim isFound as Boolean = False
            Dim isAdded as Boolean = False
            ' Get the index where prfixt is at the beginning of resultItem. If not found then, index of word which begins with prefixText.
            If InvariantLCase(autoTypeAheadSearch).equals("wordsstartingwithsearchstring") and not index = 0 Then
                ' Expression to find word which contains AutoTypeAheadWordSeparators followed by prefixText
                Dim regex1 As System.Text.RegularExpressions.Regex = new System.Text.RegularExpressions.Regex( AutoTypeAheadWordSeparators + prefixText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                If regex1.IsMatch(resultItem) Then
                    index = regex1.Match(resultItem).Index
                    isFound = True
                End If
                ' If the prefixText is found immediatly after white space then starting of the word is found so don not search any further
                If not resultItem(index).ToString() = " " Then
                    ' Expression to find beginning of the word which contains AutoTypeAheadWordSeparators followed by prefixText
                    Dim regex As System.Text.RegularExpressions.Regex = new System.Text.RegularExpressions.Regex("\\S*" + AutoTypeAheadWordSeparators + prefixText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                    If regex.IsMatch(resultItem) Then
                        index = regex.Match(resultItem).Index
                        isFound = True
                    End If
                 End If
            End If
            ' If autoTypeAheadSearch value is wordsstartingwithsearchstring then, extract the substring only if the prefixText is found at the 
            ' beginning of the resultItem (index = 0) or a word in resultItem is found starts with prefixText. 
            If index = 0 Or isFound Or InvariantLCase(autoTypeAheadSearch).Equals("anywhereinstring") then
                If InvariantLCase(AutoTypeAheadDisplayFoundText).equals("atbeginningofmatchedstring") Then
                    ' Expression to find beginning of the word which contains prefixText
                    Dim regex1 As System.Text.RegularExpressions.Regex = new System.Text.RegularExpressions.Regex("\\S*" + prefixText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                    '  Find the beginning of the word which contains prefexText
                    If (StringUtils.InvariantLCase(autoTypeAheadSearch).Equals("anywhereinstring") AndAlso regex1.IsMatch(resultItem)) Then
                        index = regex1.Match(resultItem).Index
                        isFound = True
                    End If
                    ' Display string from the index till end of the string if sub string from index till end is less than columnLength value.
                    If Len(resultItem) - index <= columnLength Then
                        If index = 0 Then 
                            itemToAdd = resultItem 
                        Else
                            itemToAdd = "..." & resultItem.Substring(index, Len(resultItem) - index) 
                        End If
                    Else
                        If index = 0 Then
                            itemToAdd = resultItem.Substring(index, (columnLength - 3)) & "..."
                        Else
                            'Truncate the string to show only columnLength - 6 characters as begining and trailing "..." has to be appended.
                            itemToAdd = "..." & resultItem.Substring(index , columnLength - 6) & "..." 
                        End If
                    End If
                ElseIf InvariantLCase(AutoTypeAheadDisplayFoundText).equals("inmiddleofmatchedstring") Then
                    Dim subStringBeginIndex as Integer = CType(columnLength/2, Integer)
                    If Len(resultItem) <= columnLength Then
                        itemToAdd = resultItem
                    Else
                        ' Sanity check at end of the string
                        If index + Len(prefixText) = columnLength Then
                            itemToAdd =  "..." & resultItem.Substring(index-columnLength,index)
                        ElseIf Len(resultITem) - index < subStringBeginIndex Then 
                            ' Display string from the end till columnLength value if, index is closer to the end of the string.
                            itemToAdd =  "..." & resultItem.Substring(Len(resultItem)-columnLength,Len(resultItem))
                        ElseIf index <= subStringBeginIndex Then 
                            ' Sanity chet at beginning of the string
                            itemToAdd =  resultItem.Substring(0, columnLength) & "..."
                        Else
                            ' Display string containing text before the prefixText occures and text after the prefixText
                            itemToAdd =  "..." & resultItem.Substring(index - subStringBeginIndex, columnLength) & "..." 
                        End If
                    End If
                ElseIf InvariantLCase(AutoTypeAheadDisplayFoundText).equals("atendofmatchedstring") Then
                     ' Expression to find ending of the word which contains prefexText
                    Dim regex1 as System.Text.RegularExpressions.Regex = new System.Text.RegularExpressions.Regex("\s", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                    ' Find the ending of the word which contains prefexText
                    If regex1.IsMatch(resultItem, index + 1) Then
                        index = regex1.Match(resultItem, index + 1).Index
                    Else
                        ' If the word which contains prefexText is the last word in string, regex1.IsMatch returns false.
                        index = resultItem.Length
                    End If
                    If index > Len(resultItem) Then
                        index = Len(resultItem)
                    End If
                    ' If text from beginning of the string till index is less than columnLength value then, display string from the beginning till index.
                    If  index <= columnLength Then
                        if index = Len(resultItem) then   'Make decision to append "..."
                            itemToAdd = resultItem.Substring(0,index)
                        Else
                            itemToAdd = resultItem.Substring(0,index) & "..."
                        End If
                    Else
                        If index = Len(resultItem) Then
                            itemToAdd = "..." & resultItem.Substring(index - (columnLength - 3), (columnLength - 3))
                        Else
                            'Truncate the string to show only columnLength - 6 characters as begining and trailing "..." has to be appended.
                            itemToAdd = "..." & resultItem.Substring(index - (columnLength - 6), columnLength - 6) & "..." 
                        End If
                    End If
                End If
                
                ' Remove newline character from itemToAdd
                Dim prefixTextIndex As Integer = itemToAdd.IndexOf(prefixText, StringComparison.InvariantCultureIgnoreCase)
                ' If itemToAdd contains any newline after the search text then show text only till newline
                Dim regex2 As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("(" & vbCrLf & "|" & vbLf & ")", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                Dim newLineIndexAfterPrefix As Integer = -1
                If regex2.IsMatch(itemToAdd, prefixTextIndex) Then
                    newLineIndexAfterPrefix = regex2.Match(itemToAdd, prefixTextIndex).Index
                End If
                If (newLineIndexAfterPrefix > -1) Then
                    If itemToAdd.EndsWith("...") Then
                        itemToAdd = (itemToAdd.Substring(0, newLineIndexAfterPrefix) + "...")
                    Else
                        itemToAdd = itemToAdd.Substring(0, newLineIndexAfterPrefix)
                    End If
                End If
                ' If itemToAdd contains any newline before search text then show text which comes after newline
                Dim regex3 As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("(" & vbCrLf & "|" & vbLf & ")", (System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.RightToLeft))
                Dim newLineIndexBeforePrefix As Integer = -1
                If regex3.IsMatch(itemToAdd, prefixTextIndex) Then
                    newLineIndexBeforePrefix = regex3.Match(itemToAdd, prefixTextIndex).Index
                End If
                If (newLineIndexBeforePrefix > -1) Then
                    If itemToAdd.StartsWith("...") Then
                        itemToAdd = ("..." + itemToAdd.Substring((newLineIndexBeforePrefix + regex3.Match(itemToAdd, prefixTextIndex).Length)))
                    Else
                        itemToAdd = itemToAdd.Substring((newLineIndexBeforePrefix + regex3.Match(itemToAdd, prefixTextIndex).Length))
                    End If
                End If

                If Not itemToAdd is nothing AndAlso Not resultList.Contains(itemToAdd) Then
                    resultList.Add(itemToAdd)
                    isAdded = true
                End If
            End If
            Return isAdded
        End Function
        

        ' To customize, override this method in EmployeesRecordControl.
        Public Overridable Sub Validate() 
            ' Initially empty.  Override to add custom validation.
        End Sub

        Public Overridable Sub Delete()
        
            If Me.IsNewRecord() Then
                Return
            End If

            Dim pk As KeyValue = KeyValue.XmlToKey(Me.RecordUniqueId)
            EmployeesTable.DeleteRecord(pk)

          
        End Sub

        Private Sub Control_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
            Try
                DbUtils.StartTransaction()

                If Not Me.Page.ErrorOnPage AndAlso (Me.Page.IsPageRefresh OrElse Me.DataChanged OrElse Me.ResetData) Then
                    Me.LoadData()
                    Me.DataBind()
                End If

            Catch ex As Exception
                Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", ex.Message)
            Finally
                DbUtils.EndTransaction()
            End Try
        End Sub
        
        

        Protected Overrides Sub LoadViewState(ByVal savedState As Object)
            MyBase.LoadViewState(savedState)
            Dim isNewRecord As String = CType(ViewState("IsNewRecord"), String)
            If Not isNewRecord Is Nothing AndAlso isNewRecord.Trim <> "" Then
                Me.IsNewRecord = Boolean.Parse(isNewRecord)
            End If
            
            Dim myCheckSum As String = CType(ViewState("CheckSum"), String)
            If Not myCheckSum Is Nothing AndAlso myCheckSum.Trim <> "" Then
                Me.CheckSum = myCheckSum
            End If
            
        End Sub

        Protected Overrides Function SaveViewState() As Object
            ViewState("IsNewRecord") = Me.IsNewRecord.ToString()
            ViewState("CheckSum") = Me.CheckSum
            
            Return MyBase.SaveViewState()
        End Function
        
        
        ' Generate the event handling functions for pagination events.
            
      
        ' Generate the event handling functions for filter and search events.
            
      
        Private _IsNewRecord As Boolean = True
        Public Overridable Property IsNewRecord() As Boolean
            Get
                Return Me._IsNewRecord
            End Get
            Set(ByVal value As Boolean)
                Me._IsNewRecord = value
            End Set
        End Property

        Private _DataChanged As Boolean = False
        Public Overridable Property DataChanged() As Boolean
            Get
                Return Me._DataChanged
            End Get
            Set(ByVal Value As Boolean)
                Me._DataChanged = Value
            End Set
        End Property

        Private _ResetData As Boolean = False
        Public Overridable Property ResetData() As Boolean
            Get
                Return Me._ResetData
            End Get
            Set(ByVal Value As Boolean)
                Me._ResetData = Value
            End Set
        End Property
        
        Public Property RecordUniqueId() As String
            Get
                Return CType(Me.ViewState("BaseEmployeesRecordControl_Rec"), String)
            End Get
            Set(ByVal value As String)
                Me.ViewState("BaseEmployeesRecordControl_Rec") = value
            End Set
        End Property
        
        Private _DataSource As EmployeesRecord
        Public Property DataSource() As EmployeesRecord
            Get
                Return Me._DataSource
            End Get
            Set(ByVal value As EmployeesRecord)
                Me._DataSource = value
            End Set
        End Property

        Private _checkSum As String
        Public Overridable Property CheckSum() As String
            Get
                Return Me._checkSum
            End Get
            Set(ByVal value As String)
                Me._checkSum = value
            End Set
        End Property
        
        Private _TotalPages As Integer
        Public Property TotalPages() As Integer
            Get
                Return Me._TotalPages
            End Get
            Set(ByVal value As Integer)
                Me._TotalPages = value
            End Set
        End Property
        
        Private _PageIndex As Integer
        Public Property PageIndex() As Integer
            Get
                ' Return the PageIndex
                Return Me._PageIndex
            End Get
            Set(ByVal value As Integer)
                Me._PageIndex = value
            End Set
        End Property
        
        Private _DisplayLastPage As Boolean
        Public Property DisplayLastPage() As Boolean
            Get
                Return Me._DisplayLastPage
            End Get
            Set(ByVal value As Boolean)
                Me._DisplayLastPage = value
            End Set
        End Property
        
        

#Region "Helper Properties"
        
        Public ReadOnly Property EmployeesTitle() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "EmployeesTitle"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property LastNameLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "LastNameLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property FirstNameLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "FirstNameLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property TitleLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "TitleLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property TitleOfCourtesyLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "TitleOfCourtesyLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property BirthDateLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "BirthDateLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property HireDateLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "HireDateLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property AddressLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "AddressLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property CityLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "CityLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property RegionLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "RegionLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property PostalCodeLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "PostalCodeLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property CountryLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "CountryLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property HomePhoneLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "HomePhoneLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property ExtensionLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "ExtensionLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property NotesLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "NotesLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property ReportsToLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "ReportsToLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property CreatedOnLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "CreatedOnLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property CreatedByLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "CreatedByLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property LastUpdatedOnLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "LastUpdatedOnLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property LastUpdatedByLabel() As System.Web.UI.WebControls.Literal
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "LastUpdatedByLabel"), System.Web.UI.WebControls.Literal)
            End Get
        End Property
        
        Public ReadOnly Property LastName() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "LastName"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property FirstName() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "FirstName"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property Title() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "Title"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property TitleOfCourtesy() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "TitleOfCourtesy"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property BirthDate() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "BirthDate"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property HireDate() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "HireDate"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property Address() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "Address"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property City() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "City"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property Region() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "Region"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property PostalCode() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "PostalCode"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property Country() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "Country"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property HomePhone() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "HomePhone"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property Extension() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "Extension"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
              Public ReadOnly Property Notes() As FredCK.FCKeditorV2.FCKeditor
              Get
              Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "Notes"), FredCK.FCKeditorV2.FCKeditor)
              End Get
              End Property
            
        Public ReadOnly Property ReportsTo() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "ReportsTo"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property CreatedOn() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "CreatedOn"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property CreatedBy() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "CreatedBy"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property LastUpdatedOn() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "LastUpdatedOn"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
        Public ReadOnly Property LastUpdatedBy() As System.Web.UI.WebControls.TextBox
            Get
                Return CType(BaseClasses.Utils.MiscUtils.FindControlRecursively(Me, "LastUpdatedBy"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property
            
#End Region

#Region "Helper Functions"

        Public Overrides Overloads Function ModifyRedirectUrl(ByVal url As String, ByVal arg As String, ByVal bEncrypt As Boolean) As String
            Dim rec As EmployeesRecord = Nothing
            
            Try
                rec = Me.GetRecord()
            Catch ex As Exception
                ' Do nothing
            End Try

            If rec Is Nothing AndAlso url.IndexOf("{") >= 0 Then
                ' Localization.
                
            Throw New Exception(Page.GetResourceValue("Err:RecDataSrcNotInitialized", "Training1"))
                    
            End If
            Return ModifyRedirectUrl(url, arg, rec, bEncrypt)
        End Function

        Public Function GetRecord() As EmployeesRecord
            If Not Me.DataSource Is Nothing Then
                Return Me.DataSource
            End If
            
            If Not Me.RecordUniqueId Is Nothing Then
                Return EmployeesTable.GetRecord(Me.RecordUniqueId, True)
            End If
            
            ' Localization.
            
            Throw New Exception(Page.GetResourceValue("Err:RetrieveRec", "Training1"))
                
        End Function

        Public Shadows ReadOnly Property Page() As BaseApplicationPage
            Get
                Return DirectCast(MyBase.Page, BaseApplicationPage)
            End Get
        End Property

#End Region

End Class

  

#End Region
    
  
End Namespace

  